<template>
  <div id="setpassword">
    <h3 class="titleTag">设置密码</h3>
    <section class="message">
      <el-form label-position="left" :inline="true" :model="ruleForm" ref="ruleForm2" :rules="rules" label-width="100px">
        <el-form-item label="账号:" prop="name">
          <el-input style="width:400px" v-model="ruleForm.name"></el-input>
        </el-form-item>
        <div class="code mb24">
          <slideVerify @sliderStatus="sliderStatus" key="1" />
        </div>
        <el-form-item label="验证码:" prop="code" key="5">
          <el-input style="width:400px" v-model="ruleForm.code">
            <template slot="append">
              <button @click="resetTime('ruleForm2')"
                style="background: none;cursor: pointer;color: #56AAFF;" :disabled="disabled">
                {{ btnText }}
              </button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="请设置密码:" prop="password1">
          <el-input style="width:400px" v-model="ruleForm.password1"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码:" prop="password1">
          <el-input style="width:400px" v-model="ruleForm.password2"></el-input>
        </el-form-item>
      </el-form>
      <div class="submitBUTTON">
        <button @click="submitTo()">保存</button>
        <button>取消</button>
      </div>
    </section>
  </div>
</template>

<script>
import slideVerify from "@/components/slideVerify.vue";
export default {
  name: "setpassword",
  components: {
    slideVerify
  },
  data() {
    return {
      status: false,
      toggle: true,
      disabled: false,
      btnText: "发送验证码",
      ruleForm: {
        name: "",
        password1: "",
        password2: "",
        code: ""
      },
      //rules前端验证
      rules: {
        name: [{ required: true, message: "请输入账号", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^(1[3-9])\d{9}$/,
            message: "输入的手机号不合法",
            trigger: "blur"
          }
        ],
        password1: [{ required: true, message: "请输入密码", trigger: "blur" }],
        password2: [{ required: true, message: "请再次输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
      }
    };
  },
  mounted() {},
  methods: {
    submitTo() {

    },

    // 倒计时
    resetTime() {
      if(!this.status) {
        this.$message({ message: "请滑动验证码验证", type: "error" });
      } else {
        console.log('object');
        this.timer()
        apiSendCode({
          phone: this.ruleForm.phone
        }).then(res => {
          console.log(res);
        }).catch(error => {
          console.log(error);
        });
      }
    },
    timer() {
      let sendSMSTime = 60;
      this.disabled = true;
      this.btnText = `已发送${sendSMSTime}s`;
      // this.timer();
      let timer = setInterval(() => {
        sendSMSTime--;
      if (sendSMSTime > 0) {
        this.btnText = `已发送${sendSMSTime}s`;
      } else {
        sendSMSTime = 0;
         clearInterval(timer);
        this.btnText = "重新获取";
        this.disabled = false;
      }
      }, 1000);
    },
    // 接受滑动验证码状态
    sliderStatus(status) {
      this.status = status;
    },
  }
};
</script>
<style lang="scss">
#setpassword {
  .el-form-item {
    margin-bottom: 30px;
  }
  .el-button {
    height: 40px;
    font-size: 20px;
    border-radius: 10px;
  }
  .el-input-group__append {
    width: 68px;
    text-align: center;
    cursor: pointer;
    background-color: #fff;
    color: $blue;
  }
}
</style>

<style lang="scss" scoped>
#setpassword {
  min-height: 100%;
  background-color: #fff;
  padding: 24px;
  box-sizing: border-box;
  .message {
    width: 550px;
    margin: 100px auto;
    .code {
      padding-left: 100px;
      width: 400px;
    }
  }
}
</style>
